package huu.phong.youtube;

import huu.phong.youtube.ConstantValue.SearchType;

import java.awt.Point;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JOptionPane;



public class Configure implements Serializable{
	private static final long serialVersionUID = 96678343791424903L;
	private transient static Configure configure;
	public List<Video> songs;
	public String title = "Youtube Desktop v1.1.2";
	public Point location = null;
	public int numberReconnect = 5;
	public String status = title + " | huuphongdn2009@yahoo.com";
	public String oldFolder = ".";
	public int lastPageSong = 1;
	public String lastValueSong = "";
	public int timeLive = 1800000;
	public boolean update = true;
	public int viewModeSong = SongRenderer.VIEW_MODE_DETAIL;
	public Format format = Format.MP4_360;
	public int resultWidth = 350;
	public int windowsState = JFrame.MAXIMIZED_BOTH;
	public int qualitySoryBoard = 3;
	public boolean displayOverlay = true;
	public FixedLinkedList<String> recentValues;
	public FixedLinkedList<Video> recentSongs;
	public SearchType searchType = SearchType.START_WITH;
	public boolean trimSearchValue = false;
	public boolean isPlayRelative = false;
	
	public synchronized static Configure getInstance(){
		if (configure == null){
			configure = load();
		}
		return configure;
	}
	
	private Configure(){
		songs = new ArrayList<Video>();
		recentValues = new FixedLinkedList<String>();
		recentSongs = new FixedLinkedList<Video>();
	}
	
	public void save(){
		if (songs.size() > 0 && (songs.get(0).getLink().equals(""))){
			songs = new ArrayList<Video>();		
		}
		try {
			ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("Youtube Desktop.conf"));
			out.writeObject(this);
			out.flush();
			out.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public static Configure load(){
		Configure that = new Configure();
		File file = new File("Youtube Desktop.conf");
		if (!file.exists()) return that;
		try {
			ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
			Object object = in.readObject();
			if (object instanceof Configure){
				Configure exist = (Configure) object;
				if (exist.title != null && exist.title.equals(that.title)){
					that = exist;
				}else{
					JOptionPane.showMessageDialog(null, "File old configure of " + exist.title +  " is not compatible with " + that.title + "\r\nOld configure will rename to " + exist.title + ".old.conf", "Loading configuare", JOptionPane.INFORMATION_MESSAGE);
					boolean rename = false;
					try {
						ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(exist.title + ".old.conf"));
						out.writeObject(exist);
						out.flush();
						out.close();
						rename = true;
					} catch (FileNotFoundException e) {
						e.printStackTrace();
					} catch (IOException e) {
						e.printStackTrace();
					}
					JOptionPane.showMessageDialog(null, "Rename is " + (rename ? "Successfull" : "Fail") + "\r\nOld configuare will replace with default configuare of " + that.title, "Loading configuare", JOptionPane.INFORMATION_MESSAGE);
				}
			}
			in.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return that;
	}
	
	public long getSerial(){
		return serialVersionUID;
	}
}
